package com.tstudy.laoshibang.community;

import com.tstudy.laoshibang.R;
import com.tstudy.laoshibang.base.BaseActivity;
import com.tstudy.laoshibang.base.BaseApplication;
import com.tstudy.laoshibang.base.BaseFragment;
import com.tstudy.laoshibang.base.CONSTANT;
import com.tstudy.laoshibang.base.CONSTANT.ARGS;
import com.tstudy.laoshibang.emojicon.EmojiconGridFragment;
import com.tstudy.laoshibang.emojicon.EmojiconsFragment;
import com.tstudy.laoshibang.emojicon.emoji.Emojicon;
import com.tstudy.laoshibang.login.RegistSuccessFragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;

/**
 * 详情页吐槽
* @CopyRight xinweitech.cn
* @author wuyahui
* @E-mail wuyahui0124505@163.com
* @version 1.0.0
* @Date 2015年4月9日 下午5:54:42
 */
public class CommunityDetailActivity extends BaseActivity implements  EmojiconGridFragment.OnEmojiconClickedListener, EmojiconsFragment.OnEmojiconBackspaceClickedListener{
	private RelativeLayout mRootLayout;
	Handler mHandler;
	String mGroupId;
	
	public static void show(boolean isOffical,int communityId,String userNo,boolean isComment,int position) {
		show(isOffical, communityId, userNo, isComment, position,null);
	}
	
	public static void show(boolean isOffical,int communityId,String userNo,boolean isComment,int position,String groupId) {
		Intent intent = new Intent(BaseApplication.mCurrentActivity,CommunityDetailActivity.class);
		intent.putExtra(CONSTANT.ARGS.IS_OFFICIAL, isOffical);
		intent.putExtra(CONSTANT.ARGS.COMMUNITY_ID, communityId);
		intent.putExtra(CONSTANT.ARGS.POSITION,position);
		intent.putExtra(CONSTANT.ARGS.IS_COMMENT, isComment);
		intent.putExtra(CONSTANT.ARGS.USER_NO, userNo);
		intent.putExtra(CONSTANT.ARGS.GROUP_ID, groupId);
		BaseApplication.mCurrentActivity.startActivity(intent);
//		BaseApplication.mCurrentActivity.overridePendingTransition(R.anim.push_in_right,R.anim.push_out_right);  
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_activity);
		mHandler = new Handler();
		mRootLayout = (RelativeLayout) findViewById(R.id.fragment_activity_root);
		mRootFragmentId = mRootLayout.getId();
		if (getIntent() != null) {
			int communityId = getIntent().getIntExtra(CONSTANT.ARGS.COMMUNITY_ID,0);
			boolean isComment = getIntent().getBooleanExtra(CONSTANT.ARGS.IS_COMMENT, false);
			int position = getIntent().getIntExtra(CONSTANT.ARGS.POSITION, 0);
			String userNo = getIntent().getStringExtra(CONSTANT.ARGS.USER_NO);
			String groupId = getIntent().getStringExtra(CONSTANT.ARGS.GROUP_ID);
			if (getIntent().getBooleanExtra(CONSTANT.ARGS.IS_OFFICIAL, false)) {
				OfficialCommunityDetailFragment.add(mRootFragmentId, communityId, userNo,isComment,position,groupId);
			}else{
				CommunityDetailFragment.add(mRootFragmentId, communityId, userNo,isComment,position,groupId);
			}
		}
	}

	@Override
	public void initArgs() {
	}

	@Override
	public void clear() {
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if(mFragmentStack.size() > 0){
				if (mFragmentStack.get(mFragmentStack.size()-1) instanceof RegistSuccessFragment) {//注册成功后不允许返回
					BaseApplication.showToast(R.string.regist_success_nick_reqired);
				}else if (mFragmentStack.get(mFragmentStack.size()-1) instanceof CommunityDetailFragment) {//退出键盘及表情layout
					CommunityDetailFragment fragment = (CommunityDetailFragment)mFragmentStack.get(mFragmentStack.size()-1);
					if (fragment.isShowInput()) {
						fragment.clear();
					}else{
						remove(mRootId,mFragmentStack.get(mFragmentStack.size()-1));
					}
				}else if (mFragmentStack.get(mFragmentStack.size()-1) instanceof OfficialCommunityDetailFragment) {//退出键盘及表情layout
					OfficialCommunityDetailFragment fragment = (OfficialCommunityDetailFragment)mFragmentStack.get(mFragmentStack.size()-1);
					if (fragment.isShowInput()) {
						fragment.clear();
					}else{
						remove(mRootId,mFragmentStack.get(mFragmentStack.size()-1));
					}
				}else{
					remove(mRootId,mFragmentStack.get(mFragmentStack.size()-1));
				}
			}else{
				finish();
			}
		}
		return false;
	}

	@Override
	public void onEmojiconBackspaceClicked(View v) {
		BaseFragment fragment = getTopFragment();
		if (fragment != null && fragment instanceof CommunityDetailFragment) {
			((CommunityDetailFragment)fragment).onEmojiconBackspaceClicked(v);
		}else if (fragment != null && fragment instanceof OfficialCommunityDetailFragment) {
			((OfficialCommunityDetailFragment)fragment).onEmojiconBackspaceClicked(v);
		}
	}

	@Override
	public void onEmojiconClicked(Emojicon emojicon) {
		BaseFragment fragment = getTopFragment();
		if (fragment != null && fragment instanceof CommunityDetailFragment) {
			((CommunityDetailFragment)fragment).onEmojiconClicked(emojicon);
		}else if (fragment != null && fragment instanceof OfficialCommunityDetailFragment) {
			((OfficialCommunityDetailFragment)fragment).onEmojiconClicked(emojicon);
		}
	}

}
